package com.example.security.demo.dto;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.security.demo.entity.SysMenu;
import com.example.security.demo.entity.SysUser;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhanpengguo
 * @date 2019-10-15 13:34
 */
@Slf4j
public class UserDto extends SysUser implements UserDetails {
    List<SysMenu> sysMenus;

    public List<SysMenu> getSysMenus() {
        return sysMenus;
    }

    public void setSysMenus(List<SysMenu> sysMenus) {
        this.sysMenus = sysMenus;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        log.info("sysMenus:{}", JSONObject.toJSONString(sysMenus));
        if (sysMenus == null) {
            return null;
        }
        Set<SimpleGrantedAuthority> collect = sysMenus.stream().map(sysMenu -> new SimpleGrantedAuthority(sysMenu.getPerms())).collect(Collectors.toSet());
        log.info("获取权限：{}", JSONObject.toJSONString(collect));
        return collect;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
